@tool
extends EditorProperty

const MODIFIERS_EDITOR_COMP = preload("res://addons/gameplay-ability-system/Scripts/Inspector/components/effect_comp/ModifiersEditorComp.tscn")

var scroll_bar = VBoxContainer.new()

func _init() -> void:
	add_child(scroll_bar)


func _ready() -> void:
	var object = get_edited_object()
	draw_modifiers(object.modifiers)


func draw_modifiers(modifiers:Array[Modifier]):
	for child in scroll_bar.get_children():
		child.queue_free()
	
	var object = get_edited_object()
	
	for modifier in object.modifiers:
		var modifiers_editor_comp = MODIFIERS_EDITOR_COMP.instantiate()
		modifiers_editor_comp.modifier = modifier
		modifiers_editor_comp.attribute_dictionary = object.attribute_dictionary
		modifiers_editor_comp.remove_modifier.connect(_no_remove_modifier)
		scroll_bar.add_child(modifiers_editor_comp)
	
	var new_modifier_btn = Button.new()
	new_modifier_btn.text = "添加新 modifier"
	new_modifier_btn.pressed.connect(_on_add_modifier_pressed)
	scroll_bar.add_child(new_modifier_btn)


func _on_add_modifier_pressed():
	var object = get_edited_object()
	if object.attribute_dictionary:
		var new_modifier = Modifier.new()
		new_modifier.attribute_to_modify = object.attribute_dictionary.attirbute_list[0].name
		object.modifiers.append(new_modifier)
		draw_modifiers(object.modifiers)
	else:
		printerr("请先为该 Effect 设置 AttributeDictionary ")

func _no_remove_modifier(modifier:Modifier):
	var object = get_edited_object()
	object.modifiers.erase(modifier)
	draw_modifiers(object.modifiers)
	
